package edu.colorado.epmapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import edu.colorado.epmapp.form.PetForm;
import edu.colorado.epmapp.models.EPMDefs;
import edu.colorado.epmapp.models.EPMDefs.PetSize;
import edu.colorado.epmapp.models.User;
import edu.colorado.epmapp.network.OnResponseListener;

public class FormTwoActivity extends Activity {

    private EditText myLocationText;
    private EditText myTagInfoText;
    private EditText myMicrochipText;
    private EditText myColorText;
    private EditText myBreedText;
    private CheckBox mySpayNeuterBox;
    private Spinner mySizeSpinner;
    
    private EPMDefs.SpayedOrNeutered  mySpayNeutered = EPMDefs.SpayedOrNeutered.Unknown;
    
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_two);

        final Context context = this;
        
        myLocationText = (EditText) findViewById(R.id.locationLostFoundText);
        myTagInfoText = (EditText) findViewById(R.id.tagInfoText);
        myMicrochipText = (EditText) findViewById(R.id.microchipText);
        myColorText = (EditText) findViewById(R.id.colorText);
        myBreedText = (EditText) findViewById(R.id.breedText);
        mySpayNeuterBox = (CheckBox) findViewById(R.id.spayNeuterBox);
        mySizeSpinner = (Spinner) findViewById(R.id.sizeSpinner);

        InputFilter locationFilter = new InputFilter.LengthFilter(EPMDefs.PETREPORT_LOCATION_LENGTH);
        myLocationText.setFilters(new InputFilter[]{locationFilter});

        InputFilter colorFilter = new InputFilter.LengthFilter(EPMDefs.PETREPORT_COLOR_LENGTH);
        myColorText.setFilters(new InputFilter[]{colorFilter});

        InputFilter breedFilter = new InputFilter.LengthFilter(EPMDefs.PETREPORT_BREED_LENGTH);
        myBreedText.setFilters(new InputFilter[]{breedFilter});

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.size_choices , android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySizeSpinner.setAdapter(adapter);

        Button submitButton = (Button) findViewById(R.id.submitBtn);
        submitButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
        submitButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                PetForm form = User.getUser().getPetForm();

                fillPetForm(form);
                
                form.submitForm(new OnResponseListener() {

                    public void onRequestComplete(String response) {

                        boolean errorFlag = false;
                        String titleText = "", msgText = "";

                        if(response == null){
                            errorFlag = true;
                            titleText = "Sorry";
                            msgText = "There may be a network problem. Please try again.";
                        }
                        else{
                            if(response.contains(EPMDefs.CREATED)){

                                Dialog submitDialog;
                                submitDialog = new Dialog(context);
                                submitDialog.setContentView(R.layout.submit_dialog);
                                submitDialog.setTitle(R.string.form_submitted);
                                submitDialog.setCancelable(false);

                                Button submitAnotherBtn = 
                                    (Button) submitDialog.findViewById(R.id.submitAnotherBtn);
                                submitAnotherBtn.setOnClickListener(new OnClickListener() {
                                    public void onClick(View v) {
                                        Intent formOneScreen = 
                                            new Intent(getApplicationContext(), FormOneActivity.class);
                                        startActivity(formOneScreen);
                                    }
                                });
                                submitDialog.show();

                            }else if(response.contains(EPMDefs.BAD_REQUEST)){
                                errorFlag = true;
                                titleText = "Submit error";
                                msgText = "Form incomplete, please complete and submit again.";

                            }else if(response.contains(EPMDefs.UNAUTHORIZED)){
                                errorFlag = true;
                                titleText = "Submit error";
                                msgText = "Please log back in and try again.";

                            }else{
                                errorFlag = true;
                                titleText = "Sorry";
                                msgText = "Your request cannot be completed at this time";
                                //DEBUG
                                msgText += " " + response;
                            }
                        }

                        if(errorFlag)
                        {
                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                            alertDialogBuilder.setTitle(titleText);
                            alertDialogBuilder.setMessage(msgText);
                            alertDialogBuilder.setCancelable(true);
                            alertDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                            AlertDialog alert = alertDialogBuilder.create();
                            alert.show();
                        }
                    }
                });
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void fillPetForm(PetForm form)
    {
        //NOTE: form one has:status, pet type, gender, imageloc, curr loc, 
        //      contact name, contact number, desc
        //NOTE: form two has: tag_id, micro_id, spayed/neutered, color, breed, 
        //      size, loc lost
        String[] sizeArr = getResources().getStringArray(R.array.size_choices);
        form.setPetSize(PetSize.valueOf(
                mySizeSpinner.getSelectedItem().toString().substring(0, 1)));
        form.setTagInfo(myTagInfoText.getText().toString());
        form.setMicrochipId(myMicrochipText.getText().toString());
        form.setSpayedOrNeutered(mySpayNeutered.toString());
        form.setPetColor(myColorText.getText().toString());
        form.setPetBreed(myBreedText.getText().toString());
        form.setLocation(myLocationText.getText().toString());
    }

    public void onCheckboxClicked(View view) {
        // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();
        
        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.spayNeuterBox:
                if (checked)
                    mySpayNeutered = EPMDefs.SpayedOrNeutered.SpayedNeutered;
                else
                    mySpayNeutered = EPMDefs.SpayedOrNeutered.Unknown;
                break;
        }
    }

}
